﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OutlookBar;
using System.Data;
using OutlookBar.Components;
using CustomControl;
using AvalonDock;
using CMISLibrary.Libraries;
using busDanhMuc;
using BaocaoNPC.common;
namespace NPCUS
{
    /// <summary>
    /// Interaction logic for Menu.xaml
    /// </summary>
    public partial class UserMenu : System.Windows.Controls.UserControl
    {
        #region Khai báo biến
        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
        public String strMa_TNgan = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap.ToUpper() : "PA";
        TreeViewItem itsel = new TreeViewItem();
        long intUserID = NPCUS_DanhMuc.Pglobal.intUserID;
        DataSet dsMenuofUser = new DataSet();
        DataSet dsMENU = new DataSet();
        DataSet dsMENUdanhmuc = new DataSet();
        Int64 intMenuidGoc = -1;
        string table_name = "";
        private object dummyNode = null;
        #endregion
        public UserMenu()
        {
            InitializeComponent();
            //dsMENUdanhmuc = new EntitiesDanhMuc.NPC_MENU();
            dsMENU = new EntitiesDanhMuc.NPC_MENU();
            get_MenuOfUser();
        }
        public event OnCreateWindow_Delegate OnCreateWindow;
        public void get_MenuOfUser()
        {
            try
            {
                dsMenuofUser = GlobalServices.Service_NPCUS.select_NPC_MENUOFUSER_by_USERID(strMa_dviqly, intUserID);
                //dsMenuofUser = clsmenuofuser.select_NPC_MENUOFUSER_by_USERID(strMa_dviqly, intUserID);
                if (dsMenuofUser == null)
                { navMainMenu.Visibility = Visibility.Hidden; return; }
                if (dsMenuofUser.Tables[0].Rows.Count > 0)
                {
                    get_Menu();
                }
                else
                {
                    navMainMenu.Visibility = Visibility.Hidden;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void get_Menu()
        {
            try
            {
                DataSet ds = new DataSet();

                for (int i = 0; i < dsMenuofUser.Tables[0].Rows.Count; i++)
                {
                    ds = GlobalServices.Service_NPCUS.select_NPC_MENU_by_ID(Convert.ToInt64(dsMenuofUser.Tables[0].Rows[i]["MENUID"]));
                    //ds = clsmenu.select_NPC_MENU_by_ID(Convert.ToInt64(dsMenuofUser.Tables[0].Rows[i]["MENUID"]));
                    //dsMENU = new DataSet();

                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        dsMENU.Tables[0].ImportRow(ds.Tables[0].Rows[0]);
                    }
                }
                if (dsMENU == null)
                { navMainMenu.Visibility = Visibility.Hidden; return; }
                if (dsMENU.Tables[0].Rows.Count > 0)
                {
                    phannhanh_menu();
                }
                else
                { navMainMenu.Visibility = Visibility.Hidden; return; }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void phannhanh_menu()
        {
            try
            {
                //dsMENUdanhmuc = new DataSet();
                DataView myView = new DataView();
                dsMENU.Tables[0].DefaultView.RowFilter = "PARENTMENUID = 0";
                myView = dsMENU.Tables[0].DefaultView;
                myView.Sort = "ORDERNUMBER ASC";
                //int i = 0;
                foreach (DataRowView dr in myView)
                {
                    DataTable dt = new EntitiesDanhMuc.NPC_MENU.NPC_MENUDataTable();
                    dt.TableName = dr["ORDERNUMBER"].ToString();
                    dsMENUdanhmuc.Tables.Add(dt);
                    intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                    fill_Children(intMenuidGoc, dr["ORDERNUMBER"].ToString());
                    //i++;
                }
                Display();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void fill_Children(long intmenu, string tablename)
        {
            try
            {
                long intMENUID = -2;
                DataRow[] drArr;
                drArr = dsMENU.Tables[0].Select("PARENTMENUID = '" + intmenu.ToString() + "'");

                DataTable dt = new DataTable();

                if (drArr.Count() > 0)
                {
                    dt = drArr.CopyToDataTable();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["MENUID"].ToString() != string.Empty)
                        {
                            intMENUID = Convert.ToInt64(dt.Rows[i]["MENUID"].ToString());

                            //menu chưa có trong dsTreeTemp -> Import
                            if (dsMENUdanhmuc.Tables[tablename].Select("MENUID = '" + intMENUID + "'").Count() < 1)
                            {
                                dsMENUdanhmuc.Tables[tablename].ImportRow(dt.Rows[i]);
                            }
                            fill_Children(intMENUID, tablename);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void Display()
        {
            try
            {
                if (dsMENUdanhmuc == null) return;
                DataView myView = new DataView();
                dsMENU.Tables[0].DefaultView.RowFilter = "PARENTMENUID = 0";
                myView = dsMENU.Tables[0].DefaultView;
                myView.Sort = "ORDERNUMBER ASC";
                foreach (DataRowView dr in myView)
                {
                    switch (dr["ORDERNUMBER"].ToString())
                    {
                        case "1":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu1.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv1);
                            break;
                        case "2":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu2.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv2);
                            break;
                        case "3":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu3.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv3);
                            break;
                        case "4":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu4.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv4);
                            break;
                        case "5":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu5.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv5);
                            break;
                        case "6":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu6.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv6);
                            break;
                        case "7":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu7.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv7);
                            break;
                        case "8":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu8.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv8);
                            break;
                        case "9":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu9.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv9);
                            break;
                        case "10":
                            table_name = dr["ORDERNUMBER"].ToString();
                            mnu10.Header = dr["HEADER"].ToString();
                            intMenuidGoc = Convert.ToInt64(dr["MENUID"]);
                            Fill_Data_Tree(dsMENUdanhmuc.Tables[dr["ORDERNUMBER"].ToString()], trv10);
                            break;
                    }
                }
                disable_menu();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void Fill_Data_Tree(DataTable dt, TreeView trv)
        {
            try
            {
                TreeViewItem root = new TreeViewItem();
                trv.Items.Clear(); // xoa tree tru?c khi d? d? li?u m?i
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show(" Không có phần tử nào trên cây !");
                    return;
                }
                DataView dv = new DataView(dt);
                //DataSet ds = new DataSet();
                //ds.Tables.Add(dt);
                //HeaderToImageConverter.Instance.ds = ds.Copy();
                if (dv.Count == 0) // không có d? li?u không dua ra tree
                {
                    MessageBox.Show(" Không có phần tử nào trên cây !");
                    return;
                }
                dv.Sort = "PARENTMENUID";
                dv.Sort = "ORDERNUMBER";

                foreach (DataRowView dr in dv)
                {
                    TreeViewItem item = new TreeViewItem();
                    if (Convert.ToInt64(dr["PARENTMENUID"]) == intMenuidGoc)
                    {
                        //Hyperlink hpl = new Hyperlink();
                        //hpl.Name = dr["HEADER"].ToString();
                        //item.Items.Add(hpl);

                        item.Header = dr["HEADER"].ToString();
                        item.Tag = dr["MENUID"].ToString();
                        item.FontWeight = FontWeights.Normal;
                        item.Items.Add(dummyNode);
                        item.MouseUp += new MouseButtonEventHandler(this.trv_SelectedItemChanged);
                        item.Expanded += new RoutedEventHandler(folder_Expanded);
                        dsMENUdanhmuc.Tables[0].DefaultView.RowFilter = "PARENTMENUID = '" + dr["MENUID"].ToString() + "'";
                        //if (dsMENUdanhmuc.Tables[0].DefaultView.Count == 0) 
                        item.IsExpanded = true;
                        trv.Items.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }
        private void folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            DataView dv = new DataView(dsMENUdanhmuc.Tables[table_name]);
            dv.Sort = "PARENTMENUID,ORDERNUMBER ASC";
            if (item.Items.Count == 1 && item.Items[0] == dummyNode)
            {
                item.Items.Clear();
                try
                {
                    foreach (DataRowView dr in dv)
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        if (dr["PARENTMENUID"].ToString() == item.Tag.ToString())
                        {
                            //Hyperlink hpl = new Hyperlink();
                            //hpl.Name = dr["HEADER"].ToString();
                            //subitem.Items.Add(hpl);

                            subitem.Header = dr["HEADER"].ToString();
                            subitem.Tag = dr["MENUID"].ToString();
                            //if (dr["TTRANG_DONGCAT"].ToString() == "0") subitem.Foreground = Brushes.Red;
                            subitem.FontWeight = FontWeights.Normal;
                            subitem.Items.Add(dummyNode);
                            subitem.MouseUp += new MouseButtonEventHandler(this.trv_SelectedItemChanged);
                            subitem.Expanded += new RoutedEventHandler(folder_Expanded);
                            dsMENUdanhmuc.Tables[0].DefaultView.RowFilter = "PARENTMENUID = '" + dr["MENUID"].ToString() + "'";
                            //if (dsResultTREE.Tables[0].DefaultView.Count == 0) 
                            subitem.IsExpanded = true;
                            item.Items.Add(subitem);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
        }
        private void trv_SelectedItemChanged(object sender, RoutedEventArgs e)
        {
            try
            {
                //TreeViewItem itsel = new TreeViewItem();
                itsel = (TreeViewItem)sender;
                dsMENU.Tables[0].DefaultView.RowFilter = "PARENTMENUID = 0";
                Int64 libId = -1;
                //itsel = ((TreeViewItem)trv.SelectedItem);


                if (itsel != null)
                {
                    dsMENU.Tables[0].DefaultView.RowFilter = "MENUID = '" + itsel.Tag.ToString() + "'";
                    if (dsMENU.Tables[0].DefaultView.Count == 0) return;
                    if (dsMENU.Tables[0].DefaultView[0]["LIBID"].ToString().Trim() != "")
                    {
                        libId = Convert.ToInt64(dsMENU.Tables[0].DefaultView[0]["LIBID"]);
                        MENU_Click(itsel, e, libId);
                        e = null;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void MENU_Click(object sender, RoutedEventArgs e, Int64 libId)
        {
            try
            {
                if (OnCreateWindow != null)
                {
                    DataSet dsTemp;
                    string strMenuid = "";
                    string strLidid = "";
                    string strNAMESPACE = "";
                    string strASSEMBLYNAME = "";
                    string strWindowName = "";
                    string strWindowTitle = "";

                    strMenuid = itsel.Tag.ToString();
                    strWindowTitle = itsel.Header.ToString();
                    dsTemp = GlobalServices.Service_NPCUS.select_NPC_LIBRARY_by_LIBID(libId);
                    //dsTemp = clslibrary.select_NPC_LIBRARY_by_LIBID(Convert.ToInt64(strLidid));

                    if (dsTemp != null && dsTemp.Tables[0].Rows.Count > 0)
                    {
                        strNAMESPACE = dsTemp.Tables[0].Rows[0]["NAMESPACE"].ToString();
                        strASSEMBLYNAME = dsTemp.Tables[0].Rows[0]["ASSEMBLYNAME"].ToString();
                        int i = strNAMESPACE.LastIndexOf(".", strNAMESPACE.Length);
                        string strTemp = strNAMESPACE.Substring(i + 1);
                        strWindowName = strTemp;

                    }
                    else return;

                    OnCreateWindow(sender, e, strWindowName, strWindowTitle, "/NPCUS;component/Images/window.png", strASSEMBLYNAME, strNAMESPACE);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khởi tạo chức năng : " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                this.Cursor = null;
            }
        }
        private void disable_menu()
        {
            try
            {
                for (int i = 10; i > 0; i--)
                {
                    dsMENU.Tables[0].DefaultView.RowFilter = "PARENTMENUID = 0 and ORDERNUMBER ='" + i + "'";
                    if (dsMENU.Tables[0].DefaultView.Count == 0)
                    {
                        navMainMenu.Sections.RemoveAt(i - 1);
                    }
                }
            }
            catch
            { }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //get_MenuOfUser();
        }
    }
}
